/**
 * @author hejl
 * @date on 2018/10/13.
 */
window.onload = function (ev) {
    topbar.hide();
	$("#userListForm .form-control").change(function(e) {
			$('#dataTable').bootstrapTable('selectPage', 1);
		});
};
var myDate = new Date();
var month = myDate.getMonth() + 1;
var day = myDate.getDate();
if (month < 10) {
    month = "0" + month;
}
if (day < 10) {
    day = "0" + day;
}
var _start = myDate.getFullYear() + '-' + month + '-' + day;
var _end = myDate.getFullYear() + '-' + month + '-' + day;
laydate.render({
    elem: '#startTime'
});
laydate.render({
    elem: '#endTime' //指定元素
});
var clickTimer = null;
var maxWidth = 5000;
var minWidth = 1;

//单击放大照片
function maxPic(event) {
    var event = window.event || event;
    var img = event.target || event.srcElement;
    if (clickTimer) {
        window.clearTimeout(clickTimer);
        clickTimer = null;
    }
    clickTimer = window.setTimeout(function () {
        var endWidth = img.width * 1.3; //每次点击后的宽度
        var endHeight = img.height * 1.3; //每次点击后的高度
        var maxTimer = setInterval(function () {
            if (img.width < endWidth) {
                if (img.width < maxWidth) {
                    img.width = img.width * 1.05;
                    img.height = img.height * 1.05;
                } else {
                    alert("已经放大到最大值了！");
                    clearInterval(maxTimer);
                }
            } else {
                clearInterval(maxTimer);
            }
        }, 20);
    }, 300);
}

// 双击缩小照片
function minPic(event) {
    var event = window.event || event;
    var img = event.target || event.srcElement;
    if (clickTimer) {
        window.clearTimeout(clickTimer);
        clickTimer = null;
    }
    var endWidth = img.width * 0.7; //每次点击后的宽度
    var endHeight = img.height * 0.7; //每次点击后的高度
    var maxTimer = setInterval(function () {
        if (img.width > endWidth) {
            if (img.width > minWidth) {
                img.width = img.width * 0.95;
                img.height = img.height * 0.95;
            } else {
                alert("已经缩小到最小值了！");
                clearInterval(maxTimer);
            }
        } else {
            clearInterval(maxTimer);
        }
    }, 20);
}
	$(function() {
		
		});
var adminRole = roleCheck();

$('#dataTable').bootstrapTable({
    url: '/UserCertificateController/queryUserCertificateList',
    height: $(window).height() - 200,
    pageSize: 30,
    pagination: true,
    pageList: [20, 50, 100],
    clickToSelect: true,
    singleSelect: true,
    queryParams: queryParams,
    queryParamsType: '',
    sidePagination: 'server',
    responseHandler: function (res) {
        return {
            "total": res.total,//总页数
            "rows": res.rows   //数据
        };
    },
    onClickRow: function (row) {
        if (row.account != null && row.account != '') {
            $("#account_field" + row.id).html(row.account);
        }
    },
    columns: [[
        {
            checkbox: true
        },
        {
            width: '3px',
            align: 'center',
            formatter: function (v, row, index) {
                return index + 1
            }
        },
        {
            title: '',
            field: 'id',
            visible: false,
            width: 100,
            align: 'center'
        }, {
            title: '会员编号',
            field: 'account',
            width: 100,
            align: 'center',
            formatter: function (v, o) {
                return copyAddon(v) + (adminRole == 1 ? v : userAccountHidden(o.account, o.id));
            }
        }, {
            title: '会员名称',
            field: 'nickName',
            width: 100,
            align: 'center'
        },
        {
            title: '认证姓名',
            field: 'name',
            width: 50,
            align: 'center'
        }, {
            title: '支付宝账号',
            field: 'alipay',
            width: 70,
            align: 'center',
        }, {
            title: '身份证号码',
            field: 'citizenIdNum',
            width: '80px',
            align: 'center'
        },
        {
            title: '已认证等级',
            field: 'successLevel',
            width: 80,
            align: 'center',
            formatter: function (v, row, index) {
                return userCertificateLevel[v];
            }
        }, {
            title: '当前申请等级',
            field: 'certLevel',
            width: 80,
            align: 'center',
            formatter: function (v, row, index) {
                return userCertificateLevel[v];
            }
        },
        {
            title: '当前等级认证状态',
            field: 'levelState',
            width: 80,
            align: 'center',
            formatter: function (v, row, index) {
                return userCertificateStatus[v];
            }
        },
        {
            title: '申请时间',
            field: 'startTime',
            width: 100,
            align: 'center'
        },
        {
            title: '修改时间',
            field: 'endTime',
            width: 100,
            align: 'center'
        }, {
            title: '备注',
            field: 'remark',
            width: 200,
            align: 'center'
        } , {
            title: '详情',
            field: 'esignature',
            width: '80px',
            align: 'center',
            formatter: function (v, row, index) {
            	var opContent ;
            	if(row.esignature&&row.esignature!=='0'){
            		opContent='<a href="javascript:void(0)" onclick=addTabFun("' + row.id + '") style="text-decoration: none;">身份证</a>';
            	}
            	if(row.contractId){
            		//opContent+='&nbsp;|&nbsp;<a href="javascript:void(0)"  onclick="handleDownloadContract(\''+row.contractId+'\')">合同</a>';
            		opContent+='&nbsp;|&nbsp;<a href="userCert/downloadContract?fileName='+row.contractId+'">合同</a>';
            	}
                return opContent;
            }
        }
    ]]
})

//查看附件详情
function addTabFun(id) {
    $("#frontPicUrl").attr("src", "");
    $("#reversePicUrl").attr("src", "");
    $("#handingPicUrl").attr("src", "");
    $("#eSignature").attr("src", "");
    $.post("/UserCertificateController/queryLevelEsignPic?id=" + id, "", function (data) {
    }).success(function (data) {
        if (data != null) {
            $("#frontPicUrl").attr("src", data[0]);
            $("#reversePicUrl").attr("src", data[1]);
            $("#handingPicUrl").attr("src", data[2]);
            $("#eSignature").attr("src", data[3]);
        }
        $("#myModal4").modal("show");
    }).error(function (data) {
    })

}

$("#search").click(function () {
    var timeAdd = new Date($("#startTime").val());
    var timeEnd = new Date($("#endTime").val());
    if (timeAdd > timeEnd) {
        layer.msg("开始时间不能大于结束时间");
        return false;
    }
    $("#dataTable").bootstrapTable("refresh");
});

function queryParams(params) {
    var startTime;
    var endTime;
    if ($("#startTime").val().length > 0) {
        startTime = $("#startTime").val() + " 00:00:00"
    } else {
        startTime = $("#startTime").val()
    }
    if ($("#endTime").val().length > 0) {
        endTime = $("#endTime").val() + " 23:59:59"
    } else {
        endTime = $("#endTime").val()
    }
    return {
        pageSize: params.pageSize,
        pageNumber: params.pageNumber,
        startTime: startTime,
        endTime: endTime,
        account: $.trim($("#account").val()),
        name: $.trim($("#name").val()),
        levelState: $("#levelState").val(),
        certLevel: $("#certLevel").val(),
        successLevel: $("#successLevel").val()
    };
}

//提交
function remarkModelShow(status) {
    $("#status").val(status);
    $("#updateUserCertificateModal").modal("show");
};

function toAddRemark() {
    var rows = $('#dataTable').bootstrapTable("getSelections");
    if (rows === null || rows === '' || rows.length === 0) {
        layer.msg("请选择要操作的数据");
        return;
    }
    $("#addRemarkModal").modal("show");
    $("#editRemark").val("");
}

function addRemark() {
    var rows = $('#dataTable').bootstrapTable("getSelections");
    if (rows === null || rows === '' || rows.length === 0) {
        layer.msg("请选择要操作的数据");
        return;
    }
    var row = rows[0];
    var remark = $("#editRemark").val();
    $.post("/UserCertificateController/addRemark?id=" + row.id + "&remark=" + remark, "", function (data) {
    }).success(function (data) {
        $("#remark").val("");
        $("#dataTable").bootstrapTable("refresh");
        layer.msg("操作成功");
    }).error(function (data) {
    })
    $("#addRemarkModal").modal('hide');
}
function handleDownloadContract(fileName){
	$.post("userCert/downloadContract?fileName="+fileName,function(data){
		console.log(data);
	});
}
/**
 * 修改实名认证状态  6成功 7失败 8处理中
 */
function updateUserCertificate() {

    var rows = $('#dataTable').bootstrapTable("getSelections");
    if (rows === null || rows === '' || rows.length === 0) {
        layer.msg("请选择要操作的数据");
        return;
    }
    var row = rows[0];
    var status = $("#status").val();
    var remark = $("#remark").val();
    $.post("UserCertificateController/updateUserCertificate?id=" + row.id + "&status=" + status + "&remark=" + remark, null, function (data) {
        if (data.success) {
            $("#status").val("");
            $("#remark").val("");
            $("#dataTable").bootstrapTable("refresh");
            layer.msg('操作成功');
        } else {
            layer.msg(data.message);
        }

    });
    $("input[name=JSESSIONID]").val(sessionStorage.getItem('JSESSIONID'));
    $("#updateUserCertificateModal").modal('hide');
};

layui.use('upload', function() {
			var upload = layui.upload;

			//执行实例
			var uploadInst = upload.render({
				elem : '#btnUploadContract', //绑定元素
				url : '/userCert/uploadContract', //上传接口
				accept : 'file',
				size : 1024 * 10,
				exts : 'pdf',
				data : {
					userid : function() {
						var rows = $('#dataTable').bootstrapTable(
								"getSelections");
						console.log(rows);
						if (rows != null && rows.length > 0) {
							return rows[0].userId;
						}
						return -1;
					}
				},
				done : function(res) {
					layui.use('layer', function() {
						var layer = layui.layer;
						layer.msg(res.message);
					});
				},
				error : function() {
					//请求异常回调
				}
			});
		});

